package com.jic.veteran.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jic.veteran.component.VeteranSecurityContext;
import com.jic.veteran.dto.ProjectDonationQueryParam;
import com.jic.veteran.mapper.ProjectDonationDetailMapper;
import com.jic.veteran.model.ProjectDonationDetail;
import com.jic.veteran.model.ProjectDonationDetailExample;
import com.jic.veteran.service.ProjectDonationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ProjectDonationServiceImpl implements ProjectDonationService {

    @Autowired
    private ProjectDonationDetailMapper projectDonationDetailMapper;


    @Override
    public List<ProjectDonationDetail> list(ProjectDonationQueryParam projectDonationQueryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        ProjectDonationDetailExample example = new ProjectDonationDetailExample();
        if(StrUtil.isNotEmpty(projectDonationQueryParam.getProjectId())) {
            example.createCriteria().andProjectIdLike("%" +projectDonationQueryParam.getProjectId() + "%");
        }
        if(StrUtil.isNotEmpty(projectDonationQueryParam.getUserId())) {
            example.createCriteria().andUserIdLike("%" +projectDonationQueryParam.getUserId() + "%");
        }
        if(StrUtil.isNotEmpty(projectDonationQueryParam.getUserName())) {
            example.createCriteria().andUserNameLike("%" +projectDonationQueryParam.getUserName() + "%");
        }
        example.setOrderByClause("donation_time desc");
        return projectDonationDetailMapper.selectByExample(example);
    }

    @Override
    public ProjectDonationDetail getItem(Long id) {
        return projectDonationDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public int create(ProjectDonationDetail projectDonationDetail) {
        projectDonationDetail.setDonationTime(DateUtil.date());
        projectDonationDetail.setCreateBy(VeteranSecurityContext.getUserName());
        projectDonationDetail.setCreateTime(DateUtil.date());
        projectDonationDetail.setModifyBy(VeteranSecurityContext.getUserName());
        projectDonationDetail.setModifyTime(DateUtil.date());
        return projectDonationDetailMapper.insert(projectDonationDetail);
    }
}
